package com.yangyuhao.haowuyoupin;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.fragment.app.Fragment;
import com.yangyuhao.haowuyoupin.fragment.HomeFragment;
import com.yangyuhao.haowuyoupin.fragment.SellFragment;
import com.yangyuhao.haowuyoupin.fragment.ProfileFragment;

public class MainActivity extends AppCompatActivity {
    private Fragment homeFragment;
    private Fragment sellFragment;
    private Fragment profileFragment;
    private Fragment activeFragment;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化Fragment
        homeFragment = new HomeFragment();
        sellFragment = new SellFragment();
        profileFragment = new ProfileFragment();

        // 设置默认Fragment
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, homeFragment)
                .add(R.id.fragment_container, sellFragment).hide(sellFragment)
                .add(R.id.fragment_container, profileFragment).hide(profileFragment)
                .commit();
        activeFragment = homeFragment;

        // 设置底部导航
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            Fragment selectedFragment = null;
            int itemId = item.getItemId();
            
            if (itemId == R.id.nav_home) {
                selectedFragment = homeFragment;
            } else if (itemId == R.id.nav_sell) {
                selectedFragment = sellFragment;
            } else if (itemId == R.id.nav_profile) {
                selectedFragment = profileFragment;
            }

            if (selectedFragment != null && selectedFragment != activeFragment) {
                getSupportFragmentManager().beginTransaction()
                        .hide(activeFragment)
                        .show(selectedFragment)
                        .commit();
                activeFragment = selectedFragment;
            }
            return true;
        });
    }
}